<?php
/**
 * 邀请好友
 * User: admin
 * Date: 2017/8/15
 * Time: 8:23
 */

namespace Api\Controller;


use Think\Controller;

class InvitingfriendsController extends Controller
{

    public function  index(){
        $uid =$_GET['uid'];
        $driver=$_GET['driver'];
        if($uid) {
            $mobile = M('ucenter_member')->where('id=' . $uid)->getField('username');
            $phone=$mobile;
            $mobile = substr_replace($mobile,'****',3,4);
            $avatar=M('member')->where('uid='.$uid)->getField('avatar');
            $avatar=M('picture')->where('id='.$avatar)->getField('url');
            $this->assign('avatar',$avatar);
            $this->assign('driver',$driver);
            $this->assign('mobile',$mobile);
            $this->assign('phone',$phone);
        }
        $this->display();
    }

    /**
     * 插入推荐人手机，获取下载地址
     */
    public function  referrer(){
        $user = I('post.user'); //下载人手机
        $driver = I('post.driver');  //下载端 1安卓 2ios
        $code = I('post.code');
        $inv_phone = I('post.inv_phone'); //推荐人手机
        $type = I('post.type');

        $map['username']            = $user;
        $map['type']                = $type;
        $info           = M('message_verify')->where($map)->order('create_time desc')->find();//查找验证码信息
        $code         = $info['code'];
        if ($_POST['code'] == $code && NOW_TIME <= $info['deadline']) { //验证码检测

        }else{
            $data['message'] ="无效验证码或验证码已过期";
            $data['code']=-4;
            json_encode($data);
            exit;
            //response(-4, '','无效验证码或验证码已过期');
        }

        $where['mobile'] = $user;
        $where['ref_mobile'] = $inv_phone;
        $res = M('referrer')->where($where)->find();
        if(!$res){ //插入推荐人表
            $data['ref_mobile']=$inv_phone;
            $data['mobile']=$user;
            $data['createtime'] =time();
            $result=M('referrer')->add($data);
        }
        $uploadurl=M('config','cmf_')->find();
        $ios_url=$uploadurl['app_ios'];
        $android_url=$uploadurl['ipa_url'];
        if($driver == 1){
            $data['upload_url'] = $android_url;
        }else{
            $data['upload_url'] = $ios_url;
        }
        response(200,$data);
    }
}